/**
 * Frame dung de keo tha ban do game
 *  
 */
package view.drag;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import view.UserHomePanel;

/**
 *
 * @author sony
 */
public class DragMapFrame extends JFrame {

    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private SaveMapDlg saveDlg;
	private UserHomePanel userHome;
	private JPanel mainPanel;
    private JPanel mousePanel;
    private JPanel mouseOutsidePanel;
    private JPanel optionPanel;
    private JPanel UpButtonPanel;
    private JPanel DownButtonPanel;
    private JLabel statusBar;
    private JButton button[][];
    private JButton OpButton[];
    private JButton AcButton[];
    private JButton btn;
    private static final int hang = 15;
    private static final int cot = 21;
    public Color MauHien = Color.YELLOW;
    public Color MauNen = Color.RED;
    public Color MauCB = Color.PINK;
    public Color MauTest = Color.MAGENTA;
    public Color CLPrepareBD = Color.PINK;
    public Color CLStart = Color.WHITE;
    public Color CLRoad = Color.GRAY;
    public Color CLMove = Color.YELLOW;
    public Color CLEnd = Color.BLUE;
    public Color CLDelete = MauNen;
    public Color CLPrepare = null;
    public Color CLClick = null;
    public Color CLEnter = null;
    public Color CLExit = null;
    public Color CLSquare[][];
    public Color CLMoveSave[][];
    public int i, j, k, l;
    public boolean[][] check;
    public boolean checkClicked;
    public boolean checkMoved;
    public boolean checkRightClicked;
    public boolean checkLeftClicked;
    public int countSet[][];
    public int countClicked;
    public int countRightClicked;
    public int MTRAN[][];
    public int SaveMTRAN[][];
    public int Temp;
    public int x1clicked, y1clicked, x2clicked, y2clicked;
    public int xmove;
    public int ymove;
    public int ytemp;
    public String FileName;
    public static int WIDTH = 900;
    public static int HEIGH = 680;
    public int WidthMouseOutSidePanel;
    public int HeighMouseOutSidePanel;
    public int WidthMousePanel;
    public int HeighMousePanel;
    public int WidthBox;
    public int HeighBox;
    public int WidthOptionPanel;
    public int HeighOptionPanel;
    public int WidthUpButtonPanel;
    public int HeighUpButtonPanel;
    public int WidthDownButtonPanel;
    public int HeighDownButtonPanel;

    /**
     *
     */
//    ------------------------------------------------------
    public DragMapFrame() {
    	saveDlg = new SaveMapDlg(this, true);
        MTRAN = new int[hang][cot];
        SaveMTRAN = new int[hang][cot];
        CLMoveSave = new Color[hang][cot];
        countSet = new int[hang][cot];
        CLSquare = new Color[hang][cot];
        button = new JButton[hang][cot];
//        -----------------------------------------------------
        OpButton = new JButton[4];
        AcButton = new JButton[5];
//        -------------------------------
//        MapName = new JLabel("Map Name:");
//        MapName.setPreferredSize(new Dimension(70, 20));
//        EditText = new JTextField();
//        EditText.setPreferredSize(new Dimension(90, 20));
////        ----------------------------
//        mapPanel = new JPanel(null);
//        mapPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
//        mapPanel.setPreferredSize(new Dimension(180, 30));
//        mapPanel.add(MapName);
//        mapPanel.add(EditText);
//        -----------------------------
        mainPanel = new JPanel();
        mainPanel.setLayout(new BorderLayout());
        mainPanel.setPreferredSize(new Dimension(WIDTH, HEIGH));
//        ----------------------
        mousePanel = new JPanel(new GridLayout(hang, cot));
        WidthBox = 35;
        HeighBox = 41;
        WidthMousePanel = WidthBox*cot;
        HeighMousePanel = HeighBox*hang;
        mousePanel.setPreferredSize(new Dimension(WidthMousePanel, HeighMousePanel));
//        ----------------------
        mouseOutsidePanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
        mouseOutsidePanel.setBackground(Color.WHITE);
        WidthMouseOutSidePanel = WIDTH*84/100;
        HeighMouseOutSidePanel = HEIGH*84/100;
        mouseOutsidePanel.setPreferredSize(new Dimension(WidthMouseOutSidePanel, HeighMouseOutSidePanel));
        mouseOutsidePanel.add(mousePanel);
//        ----------------------
        optionPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
        WidthOptionPanel = WIDTH*15/100;
        HeighOptionPanel = HEIGH;
        optionPanel.setPreferredSize(new Dimension(WidthOptionPanel, HeighOptionPanel));
//        ------------------------------
        UpButtonPanel = new JPanel(new GridLayout(0, 1));
        WidthUpButtonPanel = WidthOptionPanel*90/100;
        HeighUpButtonPanel = HeighOptionPanel*40/100;
        UpButtonPanel.setPreferredSize(new Dimension(WidthUpButtonPanel, HeighUpButtonPanel));
//        ----------------------------
        DownButtonPanel = new JPanel(new GridLayout(0, 1));
        WidthDownButtonPanel = WidthOptionPanel*90/100;
        HeighDownButtonPanel =  HeighOptionPanel*30/100;
        DownButtonPanel.setPreferredSize(new Dimension(WidthDownButtonPanel, HeighDownButtonPanel));
//        ------------------------------
//        optionPanel.add(mapPanel);
        optionPanel.add(UpButtonPanel);
        optionPanel.add(DownButtonPanel);
//        ---------------------------
        statusBar = new JLabel("Mouse outside panel");
        mainPanel.add(mouseOutsidePanel, BorderLayout.WEST);
        mainPanel.add(optionPanel, BorderLayout.EAST);
        mainPanel.add(statusBar, BorderLayout.SOUTH);
        this.add(mainPanel);
//-----------------------------------

        MouseHandler handler = new MouseHandler();


        for (int m = 0; m < hang; m++) {
            for (int n = 0; n < cot; n++) {
                CLMoveSave[m][n] = MauNen;
                countSet[m][n] = 0;
                MTRAN[m][n] = 0;
                SaveMTRAN[m][n] = 0;
            }
        }
//---------------------------------------
        for (int k = 0; k < 4; k++) {
            AcButton[k] = new JButton();
            UpButtonPanel.add(AcButton[k]);
        }

//        AcButton[0].setText("Start");
        AcButton[0].setText("Road");
        AcButton[1].setText("Move");
        AcButton[2].setText("End");
        AcButton[3].setText("Delete");

//        ---------------------------

        for (int k = 0; k < 3; k++) {
            OpButton[k] = new JButton();
            DownButtonPanel.add(OpButton[k]);
        }

        OpButton[0].setText("New");
        OpButton[1].setText("Save");
//        OpButton[2].setText("Load");
        OpButton[2].setText("Exit");

//        -----------------------------

        for (int i = 0; i < hang; i++) {
            for (int j = 0; j < cot; j++) {
                button[i][j] = new JButton();
                mousePanel.add(button[i][j]);
                CLSquare[i][j] = MauNen;
                CLSquare[0][10] = CLStart;
                CLSquare[1][10] = CLRoad;
                CLSquare[2][10] = CLRoad;
                button[i][j].setBackground(CLSquare[i][j]);
                button[i][j].addMouseListener(handler);
                button[i][j].addMouseMotionListener(handler);
            }
        }
//        ----------------------------------------
        checkMoved = false;
        CLClick = Color.RED;
        CLPrepare = CLPrepareBD;
        checkClicked = false;
        x1clicked = -1;
        y1clicked = -1;
        x2clicked = -1;
        y2clicked = -1;
        countClicked = 0;
        countRightClicked = 0;
        ClickButton();
        MouseEnterExitPressRelease();
        MouseMove();
        AcButtonListen();
        OpButtonListen();
//        ------------------------
        mouseOutsidePanel.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseEntered(MouseEvent me) {
                for (int m = 0; m < hang; m++) {
                    for (int n = 0; n < cot; n++) {
                        button[m][n].setBackground(CLSquare[m][n]);
                    }
                }
            }

            @Override
            public void mouseExited(MouseEvent me) {
            }
        });

    }
//    ----------------------

   
    /**
     * Nut New:
     * - Tao moi map
     */
    public void OpButtonListen() {
        OpButton[0].addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                for (int m = 0; m < hang; m++) {
                    for (int n = 0; n < cot; n++) {
                        CLSquare[m][n] = MauNen;
                        CLSquare[0][10] = CLStart;
                        CLSquare[1][10] = CLRoad;
                        CLSquare[2][10] = CLRoad;
                        button[m][n].setBackground(CLSquare[m][n]);
                    }
                }
            }
        });
//        ------------------------------------------------
        
        /**
         * Nut save:
         * - Luu ma tran 
         */
        OpButton[1].addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                for (int m = 0; m < hang; m++) {
                    for (int n = 0; n < cot; n++) {
                        if (CLSquare[m][n] == CLStart) {
                            MTRAN[m][n] = 64;
                        }
                        if (CLSquare[m][n] == CLRoad) {
                            MTRAN[m][n] = 16;
                        }
                        if (CLSquare[m][n] == CLMove) {
                            MTRAN[m][n] = 16;
                            if((CLSquare[m-1][n]==CLRoad)||(CLSquare[m-1][n]==CLEnd)){
                                MTRAN[m][n]+=1;
                            }
                            if((CLSquare[m][n+1]==CLRoad)||(CLSquare[m][n+1]==CLEnd)){
                                MTRAN[m][n]+=2;
                            }
                            if((CLSquare[m+1][n]==CLRoad)||(CLSquare[m+1][n]==CLEnd)){
                                MTRAN[m][n]+=4;
                            }
                            if((CLSquare[m][n-1]==CLRoad)||(CLSquare[m][n-1]==CLEnd)){
                                MTRAN[m][n]+=8;
                            }
                        }
                        if (CLSquare[m][n] == CLEnd) {
                            MTRAN[m][n] = 32;
                        }
                    }
                }
                saveDlg.setMatrix(MTRAN);
                saveDlg.setVisible(true);
                
                
            }
        });
//        ------------------------------------------------
        /**
         * Nut exit
         */
        OpButton[2].addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                setVisible(false);
            }
        });
    }

    public void AcButtonListen() {

//        AcButton[0].addActionListener(new ActionListener() {
//            @Override
//            public void actionPerformed(ActionEvent ae) {
//                CLClick = CLStart;
//                CLPrepare = CLStart;
//                if (countClicked % 2 != 0) {
//                    button[x1clicked][y1clicked].setBackground(CLClick);
//                    CLSquare[x1clicked][y1clicked] = CLClick;
//                }
//            }
//        });
//        ------------------------------------------------

        AcButton[0].addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                CLClick = CLRoad;
                CLPrepare = CLRoad;
                if (countClicked % 2 != 0) {
                    button[x1clicked][y1clicked].setBackground(CLClick);
                    CLSquare[x1clicked][y1clicked] = CLClick;
                }
            }
        });
//        ------------------------------------------------
        AcButton[1].addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                CLClick = CLMove;
                CLPrepare = CLMove;
                if (countClicked % 2 != 0) {
                    button[x1clicked][y1clicked].setBackground(CLClick);
                    CLSquare[x1clicked][y1clicked] = CLClick;
                }
            }
        });
//        ------------------------------------------------
        AcButton[2].addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                CLClick = CLEnd;
                CLPrepare = CLEnd;
                if (countClicked % 2 != 0) {
                    button[x1clicked][y1clicked].setBackground(CLClick);
                    CLSquare[x1clicked][y1clicked] = CLClick;
                }
            }
        });

//        ------------------------------------------------
        AcButton[3].addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                CLClick = CLDelete;
                CLPrepare = CLPrepareBD;
                if (countClicked % 2 != 0) {
                    button[x1clicked][y1clicked].setBackground(CLClick);
                    CLSquare[x1clicked][y1clicked] = CLClick;
                }
            }
        });


    }

    public void ClickButton() {
        for (i = 0; i < hang; i++) {
            for (j = 0; j < cot; j++) {
                button[i][j].addActionListener(new ActionListener() {
                    int i1 = i;
                    int j1 = j;

                    @Override
                    public void actionPerformed(ActionEvent ae) {
                        CLSquare[i1][j1] = CLClick;
                        CLSquare[0][10] = CLStart;
                        CLSquare[1][10] = CLRoad;
                        CLSquare[2][10] = CLRoad;
                        if (checkLeftClicked) {
                            btn = (JButton) ae.getSource();
                            btn.setBackground(CLSquare[i1][j1]);
                        }
                        if (checkRightClicked) {
                            btn = (JButton) ae.getSource();
                            btn.setBackground(CLSquare[i1][j1]);
                            countClicked++;
                            checkClicked = true;
                            MTRAN[i1][j1] = Temp;
                            if (countClicked % 2 != 0) {
                                x1clicked = i1;
                                y1clicked = j1;
                            } else {
                                x2clicked = i1;
                                y2clicked = j1;
                                if ((x1clicked == x2clicked) || (y1clicked == y2clicked)) {
                                    setCLSquare(x1clicked, y1clicked, x2clicked, y2clicked);
                                } else {
                                    countClicked++;
                                    x1clicked = x2clicked;
                                    y1clicked = y2clicked;
                                }
                            }
                        }
                    }
                });
            }
        }
    }

    public void MouseEnterExitPressRelease() {
        for (i = 0; i < hang; i++) {
            for (j = 0; j < cot; j++) {
                button[i][j].addMouseListener(new MouseAdapter() {
                    int i1 = i;
                    int j1 = j;

                    @Override
                    public void mouseClicked(MouseEvent me) {
                        Clicked(me);
                    }

                    @Override
                    public void mouseEntered(MouseEvent me) {
                        JButton source = (JButton) me.getSource();
                        if (!checkClicked) {
                            CLSquare[i1][j1] = source.getBackground();
                        }
                        if (CLSquare[i1][j1] != MauNen) {
                            source.setBackground(CLPrepare);
                        } else {
                            source.setBackground(CLPrepare);
                        }
                    }

                    @Override
                    public void mouseExited(MouseEvent me) {
                        JButton source = (JButton) me.getSource();
                        source.setBackground(CLSquare[i1][j1]);
                    }
                });
            }
        }
    }

    public void MouseMove() {
        for (i = 0; i < hang; i++) {
            for (j = 0; j < cot; j++) {
                button[i][j].addMouseMotionListener(new MouseAdapter() {
                    int i1 = i;
                    int j1 = j;

                    @Override
                    public void mouseMoved(MouseEvent me) {
                        JButton source = (JButton) me.getSource();
                        if (!checkClicked) {
                            source.setBackground(CLPrepare);
                        }

                        xmove = i1;
                        ymove = j1;
                        if (checkClicked) {
                            if (countClicked % 2 != 0) {
                                SetBackGround(x1clicked, xmove, y1clicked, ymove);
                            }
                        }
                    }
                });
            }
        }
    }

    public void Clicked(MouseEvent me) {

        if (me.isMetaDown()) {
            countRightClicked++;
            if (countRightClicked % 2 == 0) {
                checkRightClicked = false;
            } else {
                checkRightClicked = true;
            }
        } else if (me.isAltDown()) {
        } else {
            checkLeftClicked = true;
        }

    }


    public void SetBackGround(int RowClick, int RowMove, int ColClick, int ColMove) {
        if (RowClick == RowMove) {
            if (ColClick <= ColMove) {
                for (int m = ColClick; m <= ColMove; m++) {
                    button[RowClick][m].setBackground(CLClick);
                }
            }
            if (ColClick >= ColMove) {
                for (int m = ColClick; m >= ColMove; m--) {
                    button[RowClick][m].setBackground(CLClick);
                }
            }
        }
        if (RowClick != RowMove) {
            if (ColClick <= ColMove) {
                for (int m = ColClick; m <= ColMove; m++) {
                    button[RowClick][m].setBackground(CLSquare[RowClick][m]);
                }
            }
            if (ColClick >= ColMove) {
                for (int m = ColClick; m >= ColMove; m--) {
                    button[RowClick][m].setBackground(CLSquare[RowClick][m]);
                }
            }
        }
        if (ColClick == ColMove) {
            if (RowClick <= RowMove) {
                for (int m = RowClick; m <= RowMove; m++) {
                    button[m][ColClick].setBackground(CLClick);
                }
            }
            if (RowClick >= RowMove) {
                for (int m = RowClick; m >= RowMove; m--) {
                    button[m][ColClick].setBackground(CLClick);
                }
            }
        }
        if (ColClick != ColMove) {
            if (RowClick <= RowMove) {
                for (int m = RowClick; m <= RowMove; m++) {
                    button[m][ColClick].setBackground(CLSquare[m][ColClick]);
                }
            }
            if (RowClick >= RowMove) {
                for (int m = RowClick; m >= RowMove; m--) {
                    button[m][ColClick].setBackground(CLSquare[m][ColClick]);
                }
            }
        }
        button[0][10].setBackground(CLStart);
        button[1][10].setBackground(CLRoad);
        button[2][10].setBackground(CLRoad);
    }

    public void setCLSquare(int x1, int y1, int x2, int y2) {
        if (y1 == y2) {
            if (x1 <= x2) {
                for (int m = x1; m <= x2; m++) {
                    CLSquare[m][y1] = CLClick;
                }
            }
            if (x1 >= x2) {
                for (int m = x1; m >= x2; m--) {
                    CLSquare[m][y1] = CLClick;
                }
            }
        }
        if (x1 == x2) {
            if (y1 <= y2) {
                for (int m = y1; m <= y2; m++) {
                    CLSquare[x1][m] = CLClick;
                }
            }
            if (y1 >= y2) {
                for (int m = y1; m >= y2; m--) {
                    CLSquare[x1][m] = CLClick;
                }
            }
        }
        CLSquare[0][10] = CLStart;
        CLSquare[1][10] = CLRoad;
        CLSquare[2][10] = CLRoad;
    }

    private class MouseHandler implements MouseListener, MouseMotionListener {

        @Override
        public void mouseClicked(MouseEvent me) {
            statusBar.setText(String.format("Mouse Clicked at [%d,%d]", me.getX(), me.getY()));
        }

        @Override
        public void mousePressed(MouseEvent me) {
            statusBar.setText(String.format("Mouse Pressed at [%d,%d]", me.getX(), me.getY()));
        }

        @Override
        public void mouseReleased(MouseEvent me) {
            statusBar.setText(String.format("Mouse Released at [%d,%d]", me.getX(), me.getY()));
        }

        @Override
        public void mouseEntered(MouseEvent me) {
            statusBar.setText(String.format("Mouse Entered at [%d,%d]", me.getX(), me.getY()));
        }

        @Override
        public void mouseExited(MouseEvent me) {
            statusBar.setText("Mouse Outside Panel");
        }

        @Override
        public void mouseDragged(MouseEvent me) {
            statusBar.setText(String.format("Mouse Dragged at [%d,%d]", me.getX(), me.getY()));
        }

        @Override
        public void mouseMoved(MouseEvent me) {
            statusBar.setText(String.format("Moved at [" + me.getX() + "],[" + me.getY() + "]"));
        }
    }

	/**
	 * @return the saveDlg
	 */
	public SaveMapDlg getSaveDlg() {
		return saveDlg;
	}


	/**
	 * @param saveDlg the saveDlg to set
	 */
	public void setSaveDlg(SaveMapDlg saveDlg) {
		this.saveDlg = saveDlg;
	}


	/**
	 * @return the userHome
	 */
	public UserHomePanel getUserHome() {
		return userHome;
	}


	/**
	 * @param userHome the userHome to set
	 */
	public void setUserHome(UserHomePanel userHome) {
		this.userHome = userHome;
	}
}
